package com.android.lovingpet.adopt.host.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;

import com.android.lovingpet.R;
import com.android.lovingpet.adapter.MyArrayAdapter;
import com.android.lovingpet.adopt.host.HostPresenter;
import com.android.lovingpet.adopt.host.IHostContract;
import com.android.lovingpet.adopt.host.behost.view.BeHostActivity;
import com.android.lovingpet.adopt.host.hostinfo.view.HostInfoActivity;
import com.android.lovingpet.base.BaseActivity;
import com.android.lovingpet.bean.Host;
import com.android.lovingpet.common.EndLessOnScrollListener;
import com.android.lovingpet.common.OnItemClickListener;
import com.android.lovingpet.util.LocationClient;
import com.android.lovingpet.util.SpUtil;
import com.android.lovingpet.util.Utility;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HostActivity extends BaseActivity<IHostContract.View, HostPresenter> implements IHostContract.View, View.OnClickListener, OnItemClickListener, AdapterView.OnItemSelectedListener {

    @BindView(R.id.rv_host)
    RecyclerView rvHost;
    @BindView(R.id.beHost)
    Toolbar beHost;
    @BindView(R.id.sp_address)
    Spinner spAddress;
    @BindView(R.id.sp_sort)
    Spinner spSort;
    @BindView(R.id.srl_rv)
    SwipeRefreshLayout srlRv;
    private RecyclerViewAdapter rvAdapter;
    //一页显示最大的数据个数
    private final int LOAD_NUM=100;
    private int loadedCount=0;
    private String address;

    EndLessOnScrollListener endLessOnScrollListener;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_host);
        ButterKnife.bind(this);
        initView();


    }

    public void initView() {

        //address初始化
        srlRv.setRefreshing(true);
        //湖南省长沙市
        if (SpUtil.getCity() != null) {
            getPresenter().sortByAddress("长沙", 1, LOAD_NUM);
        } else {
            LocationClient.getLocation(null);
            getPresenter().sortByAddress("长沙", 1, LOAD_NUM);
        }
        address=SpUtil.getCity();
        Log.d("地址",address+"885*-");
        //下拉刷新
        srlRv.setOnRefreshListener(() -> {
            rvAdapter.clear();
            Log.d("地址",address+"/*/*-");
            //选择的是全国 获取全部host 按照时间
            if (spAddress.getSelectedItemPosition()==0){
                getPresenter().getAllHost(1,LOAD_NUM);
                return;
            }
            getPresenter().sortByAddress(address,1,LOAD_NUM);
        });

        Utility.openLocationPermission(this);

        LinearLayoutManager layoutManager=new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        rvHost.setLayoutManager(layoutManager);
        rvAdapter = new RecyclerViewAdapter(new ArrayList<>());
        rvAdapter.setOnItemClickListener(this);
        rvHost.setAdapter(rvAdapter);
        rvHost.setItemAnimator(new DefaultItemAnimator());
        spAddress.setOnItemSelectedListener(this);
        endLessOnScrollListener=new EndLessOnScrollListener(layoutManager) {
            @Override
            public void onLoadMore(int currentPage) {
                getPresenter().sortByAddress(address,loadedCount,LOAD_NUM);
            }
        };
        rvHost.setOnScrollListener(endLessOnScrollListener);


        List<String> sort_list = new ArrayList<>();
        sort_list.add("信誉度优先");
        sort_list.add("距离最近");
        sort_list.add("排序");
        MyArrayAdapter sortAdapter = new MyArrayAdapter<>(this, android.R.layout.simple_spinner_item, sort_list);
        sortAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spSort.setAdapter(sortAdapter);
        spSort.setSelection(sort_list.size() - 1, true);
        spSort.setOnItemSelectedListener(this);
    }



    @Override
    public HostPresenter createPresenter() {
        return new HostPresenter();
    }

    @Override
    public IHostContract.View createView() {
        return this;
    }

    @OnClick({R.id.beHost})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.beHost:
                startActivity(new Intent(this, BeHostActivity.class));
                break;
        }
    }

    @Override
    public void addHost(Host host) {
        runOnUiThread(() -> {
                    rvAdapter.add(host);
                    srlRv.setRefreshing(false);
                    loadedCount++;
                }
        );
    }

    @Override
    public void next() {
        runOnUiThread(()-> srlRv.setRefreshing(false));

    }

    @Override
    public void onItemClick(int position) {
        Host host = rvAdapter.getHost(position);
        EventBus.getDefault().postSticky(host);
        Intent intent = new Intent(this, HostInfoActivity.class);
        startActivity(intent);
    }

    //spinner
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()) {
            case R.id.sp_sort:
                rvAdapter.clear();
                switch (position) {
                    //信誉度优先
                    case 0:
                        loadedCount=0;
                        rvAdapter.clear();

                        srlRv.setRefreshing(true);

                        getPresenter().sortByCredit(1, LOAD_NUM);
                        break;
                    //距离最近
                    case 1:

                        break;
                }
                break;
            //地址优先
            case R.id.sp_address:
                loadedCount=0;
                address=parent.getItemAtPosition(position).toString();
                srlRv.setRefreshing(true);
                rvAdapter.clear();
                //全国
                if (position==0)
                {
                    getPresenter().getAllHost(1,LOAD_NUM);
                    return;
                }
                getPresenter().sortByAddress(address, 1, LOAD_NUM);
                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
